//
//  KZMineInformationVC.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/28.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZMineInformationVC: KZBaseViewControllerWithBack {

    let table = UITableView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "个人信息"
        setupUI()
    }
    
    override func viewSafeAreaInsetsDidChange() {
        if #available(iOS 11.0, *) {
            super.viewSafeAreaInsetsDidChange()
            table.snp.updateConstraints { (m) in
                m.bottom.equalTo(view.safeAreaInsets.bottom)
            }
        }
    }

    // MARK: ------ create views
    func setupUI() {
        
        table.backgroundColor = kBgColor
        table.estimatedRowHeight = 200
        table.rowHeight = UITableViewAutomaticDimension
        table.tableFooterView = UIView()
        table.delegate = self
        table.dataSource = self
        self.view.addSubview(table)
        table.snp.makeConstraints { (m) in
            m.left.right.top.bottom.equalTo(0)
        }
//        table .register(UITableViewCell.self, forCellReuseIdentifier: "iconCell")
//        table .register(UITableViewCell.self, forCellReuseIdentifier: "nickCell")
//        table .register(UITableViewCell.self, forCellReuseIdentifier: "mobileCell")
//        table .register(UITableViewCell.self, forCellReuseIdentifier: "TBCell")
//        table .register(UITableViewCell.self, forCellReuseIdentifier: "ZFBCell")
    }
}

extension KZMineInformationVC: UITableViewDelegate, UITableViewDataSource {
    
    // MARK: ------ tableview datasource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 2
        }
        else if section == 1 {
            return 3
        }
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            if indexPath.row == 0 {
                var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "iconCell")
                if cell == nil {
                    cell = UITableViewCell(style: .value1, reuseIdentifier: "iconCell")
                    cell?.selectionStyle = .none
                    cell?.accessoryType = .disclosureIndicator
                    cell?.textLabel?.text = "头像"
                    cell?.textLabel?.font = kFont(14)
                    
                    let iconView = UIImageView()
                    iconView.tag = 10
                    iconView.layer.cornerRadius = 25
                    iconView.layer.masksToBounds = true
                    iconView.image = kDefaultImage
                    cell?.contentView.addSubview(iconView)
                    iconView.snp.makeConstraints { (m) in
                        m.width.height.equalTo(50)
                        m.centerY.equalToSuperview()
                        m.right.equalToSuperview()
                    }
                    iconView.pictureDelegateEnable = true
                    iconView.didSelectImageClouse = { image in
                        print(image)
                    }
                }
                return cell!
            }
            
            else if indexPath.row == 1 {
                var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "nickCell")
                if cell == nil {
                    cell = UITableViewCell(style: .value1, reuseIdentifier: "nickCell")
                    cell?.selectionStyle = .none
                    cell?.accessoryType = .disclosureIndicator
                    cell?.textLabel?.text = "昵称"
                    cell?.detailTextLabel?.text = "hangzhouboye"
                    cell?.textLabel?.font = kFont(14)
                    cell?.detailTextLabel?.font = kFont(14)
                    cell?.detailTextLabel?.textColor = UIColor.lightGray
                }
                
                return cell!
            }
        }
        
        else if indexPath.section == 1 {
            if indexPath.row == 0 {
                var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "mobileCell")
                if cell == nil {
                    cell = UITableViewCell(style: .value1, reuseIdentifier: "mobileCell")
                    cell?.selectionStyle = .none
                    cell?.textLabel?.text = "手机号"
                    cell?.detailTextLabel?.text = "12345678900"
                    cell?.textLabel?.font = kFont(14)
                    cell?.detailTextLabel?.font = kFont(14)
                    cell?.detailTextLabel?.textColor = UIColor.lightGray
                }
                return cell!
            }
                
            else if indexPath.row == 1 {
                var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "TBCell")
                if cell == nil {
                    cell = UITableViewCell(style: .value1, reuseIdentifier: "TBCell")
                    cell?.selectionStyle = .none
                    cell?.textLabel?.text = "淘宝"
                    cell?.textLabel?.font = kFont(14)
                    
                    let button = UIButton(type: .custom)
                    button.tag = 10
                    button.layer.cornerRadius = 4
                    button.layer.masksToBounds = true
                    button.titleLabel?.font = kFont(14)
                    button.setTitle("绑定", for: .normal)
                    button.setTitle("解绑", for: .selected)
                    button.setBackgroundImage(kThemeColor.image, for: .normal)
                    button.setBackgroundImage(kBlueColor.image, for: .selected)
                    button.setTitleColor(UIColor.white, for: .normal)
                    cell?.contentView.addSubview(button)
                    button.snp.makeConstraints { (m) in
                        m.right.equalToSuperview().offset(-15)
                        m.width.equalTo(75)
                        m.height.equalTo(25)
                        m.centerY.equalToSuperview()
                    }
                }
                
                return cell!
            }
            
            else if indexPath.row == 2 {
                var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "ZFBCell")
                if cell == nil {
                    cell = UITableViewCell(style: .value1, reuseIdentifier: "ZFBCell")
                    cell?.selectionStyle = .none
                    cell?.textLabel?.text = "支付宝"
                    cell?.textLabel?.font = kFont(14)
                    
                    let button = UIButton(type: .custom)
                    button.tag = 10
                    button.isSelected = true
                    button.layer.cornerRadius = 4
                    button.layer.masksToBounds = true
                    button.titleLabel?.font = kFont(14)
                    button.setTitle("绑定", for: .normal)
                    button.setTitle("解绑", for: .selected)
                    button.setBackgroundImage(kThemeColor.image, for: .normal)
                    button.setBackgroundImage(kBlueColor.image, for: .selected)
                    button.setTitleColor(UIColor.white, for: .normal)
                    cell?.contentView.addSubview(button)
                    button.snp.makeConstraints { (m) in
                        m.right.equalToSuperview().offset(-15)
                        m.width.equalTo(75)
                        m.height.equalTo(25)
                        m.centerY.equalToSuperview()
                    }
                }
                
                return cell!
            }
        }
        
        return UITableViewCell()
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0, indexPath.row == 0 {
            return 70
        } else {
            return 44
        }
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10

    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     
        if indexPath.section == 0, indexPath.row == 1 {
            let vc = KZMedifyNicknameVC()
            vc.saveSuccessClouse = {
                print($0)
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}
